using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web;
using Microsoft.SharePoint;
using System.Web.UI;
using System.ComponentModel;

namespace ARF.Web.Controls
{
    [ToolboxData("<{0}:QSVisibilityPanel runat=\"server\" />"), ToolboxItemFilter("ARF")]
    public class QSVisibilityPanel : Panel
    {
        private string _queryStringParameter;
        private string _parameterValue;
        private bool _defaultPanel;
        private bool _renderWrapper = false;

        public bool RenderWrapper
        {
            get { return _renderWrapper; }
            set { _renderWrapper = value; }
        }

        public bool DefaultPanel
        {
            get { return _defaultPanel; }
            set { _defaultPanel = value; }
        }

        public string ParameterValue
        {
            get { return _parameterValue; }
            set { _parameterValue = value; }
        }

        public string QueryStringParameter
        {
            get { return _queryStringParameter; }
            set { _queryStringParameter = value; }
        }

        public override bool Visible
        {
            get
            {
                return IsVisible;
            }
            set
            {
                base.Visible = value;
            }
        }

        protected virtual bool IsVisible
        {
            get
            {
                if (SPContext.Current.FormContext.FormMode != Microsoft.SharePoint.WebControls.SPControlMode.Display)
                    return true;

                if (string.IsNullOrEmpty(QueryStringParameter))
                {
                    // Always need a query string specified
                    Log.Trace("QSVisibilityPanel: No value specified");
                    return false;
                }

                if (string.IsNullOrEmpty(ParameterValue))
                {
                    // If there is no Value and the query string isn't present, render otherwise prevent rendering
                    // This is the default panel to show
                    if (HttpContext.Current.Request[QueryStringParameter] == null)
                        return base.Visible;
                    else
                        return false;
                }

                if (HttpContext.Current.Request[QueryStringParameter] == null)
                {
                    if (DefaultPanel)
                        return base.Visible;
                    else
                        return false;
                }

                if (HttpContext.Current.Request[QueryStringParameter] == ParameterValue.ToLower())
                    return base.Visible;

                return false;
            }
        }

        public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
        {
            if (RenderWrapper) base.RenderBeginTag(writer);
        }

        public override void RenderEndTag(System.Web.UI.HtmlTextWriter writer)
        {
            if (RenderWrapper) base.RenderEndTag(writer);
        }
    }
}
